package warehouse

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetWarehousePageApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetWarehousePageApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetWarehousePageApiLogic {
	hostname, _ := os.Hostname()
	return &GetWarehousePageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetWarehousePageApiLogic) GetWarehousePageApi(req *types.GetWarehousePageReq) (resp *types.GetWarehousePageResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return &types.GetWarehousePageResp{
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
			Data: make([]types.WarehouseResp, 0), // 表格数据
		}, nil
	}
	fmt.Println(req.PageSize, req.PageNumber, "传递数据")
	warehouseBo, count, err := service.WarehouseService.GetWarehousePageApi(l.ctx, bo.WarehousePageReqBo{
		PageSize:     req.PageSize,   // 当前条数
		PageNumber:   req.PageNumber, // 当前页数
		Name:         req.Name,       // 仓库名称或者编码
		Status:       req.Status,     // 仓库状态
		EnterpriseId: accountInfo.EnterpriseId,
	})
	if err != nil {
		return &types.GetWarehousePageResp{
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
			Data: make([]types.WarehouseResp, 0), // 表格数据
		}, nil
	}
	warehouseResp := k.Map(warehouseBo, func(item bo.WarehouseBo, index int) types.WarehouseResp {
		return types.WarehouseResp{
			ID:         item.ID,                                 // 主键id
			Name:       item.Name,                               // 仓库名称
			Code:       item.Code,                               // 仓库编码
			Status:     item.Status,                             // 状态0表示正常,1表示禁用
			IsDefault:  item.IsDefault,                          // 是否为默认仓库,0表示不是，1表示是
			PersonID:   item.PersonID,                           // 仓库负责人,关联到enterprise_person表主键id
			PersonName: item.PersonName,                         // 仓库负责人,关联到enterprise_person表主键id
			Describe:   item.Describe,                           // 仓库名称描述
			CreatedAt:  utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:  utils.TimestampToString(item.UpdatedAt),
		}
	})
	return &types.GetWarehousePageResp{
		PageBaseResp: types.PageBaseResp{
			Total:      count,
			PageSize:   req.PageSize,
			PageNumber: req.PageNumber,
		},
		Data: warehouseResp, // 表格数据
	}, nil
}
